package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	import gs.TweenFilterLite;
	
	import net.L4cd.game.iLLK;


	[SWF(width="800", height="600", frameRate="24", backgroundColor="0x123456")]
	public class LinkGame extends Sprite
	{
		private var x_max:uint=16;
		private var y_max:uint=12;
		private var ico_w:uint=30;
		private var ico_h:uint=30;
		private var icon_max:uint=12;
		private var ex_x:uint=0;
		private var ex_y:uint=45;
		private var llk:iLLK=new iLLK();
		private var obj1:MovieClip;
		private var obj2:MovieClip;
		private var liner:Sprite=new Sprite();
		private var mc_map:MovieClip=new MovieClip();
		
		private var hint:btnHint=new btnHint();
		private var onceAgin:btnOnceAgin=new btnOnceAgin();
		public function LinkGame()
		{

			stage.scaleMode="noScale";
			addChild(mc_map);
			createMap(null);
			hint.addEventListener(MouseEvent.CLICK, abcd);
			onceAgin.addEventListener(MouseEvent.CLICK, createMap);

			addChild(liner);
		}

		private function getIconArr():Array
		{
			var _icons:Array=new Array();
			var _max:uint=(x_max - 2) * (y_max - 2);
			var _num:uint=Math.floor(_max / icon_max);
			_num+=_num % 2 == 1 ? -1 : 0;
			for (var i:uint=0; i < _max; i++)
			{
				if (Math.floor(i / _num) < icon_max - 1)
				{
					_icons.push(Math.floor(i / _num));
				}
				else
				{
					_icons.push(icon_max - 1);
				}

			}
			return _icons;
		}

		private function createMap(e:MouseEvent):void
		{
			var map:Array=new Array();
			var icons:Array=getIconArr();
			while (mc_map.numChildren > 0)
			{
				var mc:MovieClip=mc_map.getChildAt(0) as MovieClip;
				mc.removeEventListener(MouseEvent.MOUSE_DOWN, abc);
				mc_map.removeChild(mc);
			}
			for (var x_:uint=0; x_ < x_max; x_++)
			{
				var map_:Array=new Array();
				for (var y_:uint=0; y_ < y_max; y_++)
				{
					var icon:int;
					if (x_ == 0 || y_ == 0 || x_ == x_max - 1 || y_ == y_max - 1)
					{
						icon=-1;
					}
					else
					{
						icon=icons.splice(random(0, icons.length - 1), 1)[0];
					}
					var obj:Object={x: x_, y: y_, type: icon};
					map_[y_]=obj;
					var k:MovieClip=new k_icon();
					k.gotoAndStop(icon + 2);
					k.light.visible=false;
					k.shine.visible=false;
					k.buttonMode=true;
					k.mouseChildren=false;
					k.x_=x_;
					k.y_=y_;
					k.x=ex_x + ico_w * x_;
					k.y=ex_y + ico_h * y_;
					k.name="x" + x_ + "y" + y_;
					k.addEventListener(MouseEvent.MOUSE_DOWN, abc);
					mc_map.addChild(k);
				}
				map[x_]=map_;
			}
			llk.map=map;
		}

		private function abcd(e:MouseEvent):void
		{
			var p:Array=llk.getLink();
			if (p)
			{
				//(mc_map.getChildByName("x" + p[0][0] + "y" + p[0][1]) as MovieClip).shine.visible=true;
				//(mc_map.getChildByName("x" + p[1][0] + "y" + p[1][1]) as MovieClip).shine.visible=true;
			}
		}

		private function abc(e:MouseEvent):void
		{
			var k:MovieClip=e.currentTarget as MovieClip;
			if (!obj1)
			{
				obj1=k;
				obj1.light.visible=true;
			}
			else
			{
				obj2=k;
				obj2.light.visible=true;
				if (obj1 == obj2)
				{
					obj1.light.visible=false;
					obj2.light.visible=false;
					obj1=obj2=null;
					return;
				}
				var path:Array=llk.link([obj1.x_, obj1.y_], [obj2.x_, obj2.y_]);
				if (!path)
				{
					obj1.light.visible=false;
					obj2.light.visible=false;
					obj1=obj2=null;
					return;
				}
				obj1.mouseEnabled=false;
				obj2.mouseEnabled=false;
				obj1.gotoAndStop(1);
				obj2.gotoAndStop(1);
				//画线
				liner.graphics.clear();
				liner.graphics.lineStyle(2, 0xFFFFFF);
				liner.graphics.moveTo(obj1.x + ico_w / 2, obj1.y + ico_h / 2);
				for (var i:uint=0; i < path.length; i++)
				{
					var mc:MovieClip=mc_map.getChildByName("x" + path[i][0] + "y" + path[i][1]) as MovieClip;
					liner.graphics.lineTo(mc.x + ico_w / 2, mc.y + ico_h / 2);
				}
				liner.graphics.lineTo(obj2.x + ico_w / 2, obj2.y + ico_h / 2);
				liner.alpha=1;
				TweenFilterLite.to(liner, 0.5, {alpha: 0, glowFilter: {color: 0xFFFFFF, alpha: 3, blurX: 10, blurY: 10, strength: 2, quality: 3}});
				obj1.light.visible=false;
				obj2.light.visible=false;
				obj1.shine.visible=false;
				obj2.shine.visible=false;
				obj1.bobo.gotoAndPlay(5);
				obj2.bobo.gotoAndPlay(5);
				obj1=obj2=null;
			}

		}


		private function random(min:int, max:int):int
		{
			if (min == max)
			{
				return min;
			}
			else if (min > max)
			{
				return max + int(Math.random() * (min - max + 1));
			}
			else
			{
				return min + int(Math.random() * (max - min + 1));
			}

		}
	}
}
